home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / packet / p_tapr / tnchst / mthcode.c < prev    next >
Text File  |  1991-08-18  |  13KB  |  514 lines

  1. /** MTHOST - TNC-2 binary interface main module
  2.     (c) 1989-91 Howard Goldstein **/
  3.     
  4.    extern void near cdecl COM_START(void);
  5.    extern void near cdecl REMOVE_VECTOR(void);
  6.    extern int near cdecl FILETOTNC(char *);
  7.  
  8.  
  9.  
  10. #define   BIT8N   FALSE
  11.  
  12.    union REGS regblock, retblk ,*auxr = ®block ;      /* for intdos call */
  13.  
  14.  
  15.  
  16.     FILE    *upfile, *dwnfile ;    /* file pointers */
  17.  
  18.     char    fn[80]     ;            /* store filenames here */
  19.     char    multi_name[20]    ;        /* filename for multi
  20.                            downlaods here */
  21.  
  22.     long    free_for_download ;    /* limit to amount free to dwnld */
  23.  
  24.         int    tnc, kbd     ;
  25.  
  26.     char    *dmys        ;        /* dummy string ptr, for
  27.                            lint in set_up/dwn */
  28.     int    dmy ;                /* dmy int var */
  29.  
  30.  
  31.     /** DLC ADDRESS **/
  32.     byte    mydlcnum = 0 ;    /* must be 0<num<254.  TNCs should start at 1,
  33.                            hosts at 254.  Addressing off = 0 */
  34.  
  35.  
  36. long int disk_free()
  37. {
  38.     auxr->h.ah = 0x36 ;            /* disk space */
  39.     auxr->h.dl = 0 ;        /* get curr drive */
  40.     intdos( auxr , &retblk );        /* get space */
  41.  
  42.     return (  (long) retblk.x.ax * (long) retblk.x.bx *
  43.                   (long) retblk.x.cx ) ;
  44.                     /* return the free space */
  45. }
  46.  
  47.  
  48. void updonemsg()
  49. {
  50.     printf("\007\n*Upload complete*\n");
  51. }
  52.  
  53.  
  54. int donada( dmy )
  55. int dmy;
  56. {
  57.    signal( SIGINT, donada  ) ;
  58. PUTAUX(3); return(3); }
  59.  
  60.  
  61.  
  62. void print_help()            /* print help info */
  63. {
  64.     printf("\
  65. \r\n\r\n       TNC-2 binary intfc test terminal program (11 Jan 91 n2wx)\
  66. \r\n\r\n");
  67.  
  68.     printf("\r\n\
  69.   Key                 Function\r\n\
  70.   ---                 --------\r\n\
  71.   PGUP            Upload a file to the TNC\r\n");
  72.     printf("\
  73.   PGDWN           Download a file from the TNC\r\n\
  74.    (note upload and download are not as they seem and irrelevant re: BLP\r\n\
  75. \r\n\
  76.   ESC             Close any open upload and/or download files\r\n\
  77.   F1              Set DLC Address (0=dlc addressing off, 1-254 valid)\r\n");
  78. printf("\
  79.   F2              Initiate Connect (cnct to TNCCMD to get cmd\r\n\
  80.   F3              Send data on a BLP channel\n\r\
  81.   F4              Disconnect\n\r\
  82.   F5              Send the canned UI frame (A>B:xxxx)\n\r\
  83.   F6              Send conn-stat-enq to selected channel\n\r\
  84. \r\n\
  85.   Alt-X           Exit the program, return to DOS\r\n\r\n\r\n");
  86.  
  87. }
  88.  
  89.  
  90.  
  91.  
  92. void set_upfile()
  93. {
  94.     static struct timeb lasttimeb, thistimeb ; /* keep track of secs */
  95.     static unsigned long filsiz ;        /* track filesize too */
  96.        double lasttime,thistime;
  97.     double cps ;                /* chars per sec computed */
  98.     double rola = 0.0;            /* rolling avg accum */
  99.     FILE *ts;    /* temporary file ptr for checking size */
  100.  
  101.     if ( upfile )                /* already doing it? */
  102.         return ;            /* yes, ignore upld cmd */
  103.  
  104.     printf("\r\nFile to upload:");
  105.     dmys = gets(fn);            /* get the string */
  106. /*    if ( strlen( fn ) > 0 )            /* if somethign here */
  107.  
  108.     if ( ! ( ts = fopen( fn , "r" ) ) )
  109.     {
  110.         set_upfile(); return; /* recursively call and ret if err */
  111.     }
  112.  
  113.     filsiz = filelength( fileno( ts ) ) ;    /* get long file len */
  114.     fclose( ts) ;
  115.     ftime(&thistimeb);        /* initialize so as to not bias
  116.                        the moving average too much */
  117. #if 1
  118.        do
  119.     {
  120.             memcpy((char *) &lasttimeb,(char *) &thistimeb,
  121.                            sizeof(struct timeb) );
  122.                 /* get secs fm last */
  123.             ftime(&thistimeb);        /* get # secs now */
  124.  
  125.  lasttime = ((double) lasttimeb.time)+  ((double)lasttimeb.millitm /1000.0);
  126.  thistime = ((double) thistimeb.time)+  ((double) thistimeb.millitm /1000.0);
  127.  
  128.                 /* compute cps as a double */
  129.             if ( lasttimeb.time==thistimeb.time )
  130.             {    rola = cps = 960.0 ; }
  131.             else
  132.                 cps = (double) filsiz /
  133.                       (thistime-lasttime) ;
  134.  
  135.                 /* compute rolling avg */
  136.             rola = (rola * .8) + ( cps * .2 ) ;
  137.  
  138. printf("%lu chars in %.2f seconds = %.1fcps, avg %.2f\n",
  139.                 filsiz,  thistime-lasttime, cps , rola );
  140.  
  141.  
  142. /*        updonemsg();        /* uplaod done if came back ok */
  143.     }
  144.     while ( ftime(&thistimeb), ! FILETOTNC(fn) && !kbhit() ) ;
  145.  
  146. #else
  147.       if ( ! ( upfile = fopen( fn , "rb" ) ) ) /* and if NOT able to open */
  148.       {
  149.         printf("%s not found!\r\n",fn) ;
  150.         set_upfile()              ;  /* show err and do again */
  151.       }
  152. #endif
  153.  
  154. }
  155.  
  156.  
  157. void set_dwnfile()
  158. {
  159.     if ( dwnfile )                /* already doing it? */
  160.         return ;            /* yes, ignore upld cmd */
  161.  
  162.     free_for_download = disk_free() - MINFREE ;    /* lv something */
  163.  
  164.     printf("\r\nFile to download:");
  165.     dmys = gets( fn ) ;            /* get the string */
  166.     if ( strlen( fn ) > 0 )            /* if somethign here */
  167.       if ( ! ( dwnfile = fopen( fn , "wb" ) ) ) /* and if NOT able to open */
  168.       {
  169.         printf("Can't open %s!\r\n",fn) ;
  170.         set_dwnfile()              ;  /* show err and do again */
  171.       }
  172. }
  173.  
  174.  
  175.  
  176.  
  177. void cdecl kbd_blp_call()        /* issue outgoing call */
  178. {
  179.     struct blpcb_struct *blpcb;    /* get new blpcb from blp */
  180.     char addr[80];
  181.  
  182.         printf("Enter address to call; precede with '!' to get email:");
  183.     dmys = gets( addr );
  184.  
  185.     blpcb = blp_start_cmd( nextchan, addr );    /* try to setup */
  186.  
  187.     if ( blpcb )            /* if was able to set up */
  188.     {
  189.                 blp_intfc[nextchan] = blpcb ;    /* save ptr to intfc */
  190.         nextchan++;        /* bump to next chan */
  191.         printf("calling...");
  192.     }
  193.     else printf("*** kbd_blp_call(): couldn't start a blp\n");
  194. }
  195.  
  196. void cdecl kbd_blp_data()        /* send outgoing data */
  197. {
  198.     char    temp[256],data[258];
  199.         int    send_stat;    /* return stat from send attempt */
  200.     int    chan;    /* chan # to send to */
  201.  
  202.     printf("chan # [space] Data (max 256):");
  203.     dmys = gets( temp );
  204.  
  205.     if (strlen(temp) < 3 )
  206.         strcpy(temp,"0  ");
  207.         
  208.             /* get chan # and store data */
  209.     sscanf( temp, "%d", &chan );
  210.  
  211.             /* cr append if not cmd channel */
  212.     switch ( blp_intfc[chan]->lcn )
  213.     {    case 0x70: case 0x71: case 0x72:
  214.             break;
  215.         default:
  216.             strcat( temp , "\r" );    /* force cr on end */
  217.     }
  218.             /* send the data */
  219.     send_stat = blp_data_cmd( blp_intfc[chan], temp+2, strlen(temp+2) );
  220.  
  221.     if ( !send_stat )    /* if err */
  222.         printf("Error sending data\n");
  223. }
  224.  
  225.  
  226. #if 0
  227.     byte    stdframe[] = { 
  228.                          0x84,0x40,0x40,0x40,0x40,0x40,0x60,
  229.                          0x84,0x40,0x40,0x40,0x40,0x40,0x60,
  230.                          0x82,0x40,0x40,0x40,0x40,0x40,0x61,
  231.                          0x03,0xf0,
  232. "1234567890123456789012345678901234567890123456789012345678901234567890\
  233. 1234567890123456789012345678901234567890123456789012345678901234567890\
  234. 1234567890123456789012345678901234567890123456789012345678901234567890\
  235. 1234567890123456789001234567890test\r"};
  236. #else
  237. /* tiny test frame*/
  238.     byte    stdframe[] = { 
  239.                          0x84,0x40,0x40,0x40,0x40,0x40,0x60,
  240.                          0x84,0x40,0x40,0x40,0x40,0x40,0x60,
  241.                          0x82,0x40,0x40,0x40,0x40,0x40,0x61,
  242.                          0x03,0xf0,"\
  243. 012345678901234567890123456789012345678\r"};
  244.  
  245.  
  246. #endif
  247.  
  248. void cdecl kbd_blp_datagram()
  249. {
  250.     char    temp[256],data[258];
  251.  
  252.         int    send_stat,chan;    /* return stat from send attempt */
  253.  
  254. /*    printf("lcn #:");
  255.     dmys = gets( temp );
  256.     if (strlen(temp) < 3 )
  257.         strcpy(temp,"0  ");
  258.     sscanf( temp, "%d", &chan );
  259.  */
  260.     
  261.     printf("sending test packet to channel 113d (71H)\n");
  262.     chan = 0x71;
  263.  
  264.             /* send the data */
  265.     send_stat = send_blp_datagram( chan, stdframe, sizeof( stdframe ));
  266.  
  267.     if ( !send_stat )    /* if err */
  268.         printf("Error sending data\n");
  269. }
  270.  
  271. void cdecl kbd_blp_stop()
  272. {
  273.     char    temp[20];
  274.     int    chan;
  275.  
  276.     printf("chan # to stop:");
  277.     dmys=gets(temp);
  278.     sscanf(dmys,"%d",&chan);
  279.     blp_stop_cmd( blp_intfc[chan] );
  280. }
  281.  
  282. void cdecl kbd_send_cstenq()
  283. {
  284.     char    temp[20];
  285.     int    chan;
  286.  
  287.     printf("chan # to enquire:");
  288.     dmys=gets(temp);
  289.     sscanf(dmys,"%d",&chan);
  290.     blp_status_cmd( blp_intfc[chan] );
  291. }
  292.  
  293. void cdecl set_dlc_addr()
  294. {
  295.     char    temp[20];
  296.  
  297.     printf("Address is now %d.  New address: ",mydlcnum);
  298.     dmys = gets(temp);
  299.     sscanf(dmys,"%d",&mydlcnum);
  300. }
  301.  
  302. void cdecl cmdproc( firstc )
  303. int firstc;                          /* enter with 0 for extended or ESC */
  304. {                    /* for escape */
  305.  
  306.     void cdecl paint_disk_file();    /* later in this module, invokes
  307.                        paint fm file procedure */
  308.     void cdecl paint_printer();    /* same as above */
  309.  
  310.     static    char fn[ 80 ] ;        /* store fn here, static so we don't
  311.                        stack overflow too soon on recurs */
  312.         static    int  xkey ;        /* if extended, nxt code put here */
  313.  
  314.     if ( firstc == ESC )        /* if it was escape */
  315.     {
  316.         if ( dwnfile )
  317.         {
  318.             fputc( EOFCH , dwnfile );
  319.             fclose( dwnfile );    /* close up file, if open */
  320.         }
  321.         if ( upfile )
  322.         {
  323.             printf("\007\n*Upload aborted*\n");
  324.             fclose( upfile ); /* down file too */
  325.         }
  326.         dwnfile = upfile = (FILE *) NULL ;
  327.                         /* zap pointers */
  328.  
  329.     }
  330.     else if ( firstc == 0 )        /*   was extended char ?*/
  331.     {
  332.         xkey = getch() ;    /* get the extended key */
  333.  
  334.         switch ( xkey )        /* based on extended key... */
  335.         {
  336.           case PGUP:                /* upload */
  337.                 set_upfile() ; break ;
  338.           case PGDWN:
  339.                 set_dwnfile(); break ;
  340.           case F1:              
  341.                 set_dlc_addr();break;    /* set address */
  342.           case F2:                
  343.                 kbd_blp_call(); break; /* call setup */
  344.  
  345.           case F3:                
  346.                 kbd_blp_data(); break; /* send blp data */
  347.                   case F4:
  348.                 kbd_blp_stop();
  349.                                 break;
  350.              case F5:    /* send datagram */
  351.                  kbd_blp_datagram(); break;
  352.             case F6:    /* send cstenq */
  353.                 kbd_send_cstenq(); break;
  354.           case ALTX:
  355.                                 cmdproc( ESC ) ;    /* close files */
  356.                 REMOVE_VECTOR();    /* remove vec */
  357.                 exit(0)        ;    /* bye */
  358.           default:
  359.                 print_help(); break ;
  360.         }
  361.     }
  362. }
  363.  
  364.  
  365.  
  366. void totnc_proc()
  367. {
  368.  
  369.  
  370.     if ( kbhit() )
  371.     {
  372.         kbd = getch();            /* just get the char,
  373.                        used to echo directly */
  374.             putchar( kbd );        /* echo if not paint */
  375.  
  376.  
  377.         switch ( kbd )
  378.         {
  379.             case 0:
  380.             case ESC:
  381.                 cmdproc( kbd ) ;
  382.                 break ;
  383.                 /* if extended key or */
  384.                 /* escape */
  385.                 /*   then proc as cmd */
  386.             case CR:
  387.                               putchar('\n');    /* echo newline */
  388.                         /* fall thru too */
  389.             default:
  390.                 PUTAUX( kbd ) ;    /*   else send to tnc*/
  391.         }
  392.  
  393.     }
  394.  
  395.     while ( upfile && AUXRDY() )            /* upldng and rdy? */
  396.     {
  397.         kbd = fgetc( upfile ) ;
  398.         if ( kbd != EOFCH )            /* not EOF?    */
  399.         {
  400.                 PUTAUX( kbd );
  401.         }
  402.         else /* eof */
  403.         {
  404.  
  405.  
  406. /*            fclose( upfile ) ;            */
  407. /*               updonemsg();            /* say upload complete*/
  408. /*            upfile = (FILE *) NULL ;        */
  409.  
  410.                 /* keep going till abort */
  411.  
  412.  
  413.         }
  414.     }
  415. }
  416.  
  417.  
  418.  
  419.  
  420. void fmtnc_proc()
  421. {
  422.     if ( AUXAVAIL() )                /* if fm tnc rdy */
  423.     {
  424.         tnc = ( GETAUX() ) ;        /* get char */
  425.             putchar( tnc );
  426.  
  427.  
  428.         aftr_proc( tnc );        /* send to aft handler */
  429.  
  430.         if ( dwnfile )                /* dwnldng? */
  431.         {
  432.             fputc( tnc , dwnfile ) ;    /* send it if so */
  433.             if ( free_for_download-- < 0L )    /* if disk full */
  434.             {
  435.                 fclose( dwnfile );
  436.                 dwnfile = NULL ;
  437. printf("\n\r\007DISK FULL!, minimum %d bytes needed.  Download cancelled.\r\n",
  438.    MINFREE);
  439.             }
  440.         }
  441.     } /*endif*/
  442. }
  443.  
  444.  
  445. blp_tick_test()
  446. {
  447.     static    long lasttime;
  448.         long thistime ;
  449.   /** upfile holdoff below -- DONT put in normal host, use it here
  450.       so n2wx can continue to use this prog for timing... **/
  451.       
  452.     if ( !upfile && time(&thistime) != lasttime )    /* if a second elapsed */
  453.     {
  454.         lasttime = thistime ;        /* update time */
  455.         blp_timer_tick();        /* do the tick */
  456.         dlc_timer_tick();
  457.     }
  458. }
  459.  
  460. main(argc,argv)
  461. int argc;
  462. char *argv[];
  463. {
  464.  
  465.    int x;
  466.  
  467.  
  468.        COM_START();                    /* init the COM1
  469.                                hardware/driver */
  470.  
  471.    auxr->x.dx = COMPORT ;                                    /* com1 */
  472.    auxr->h.ah = 0 ;                       /* init */
  473.  
  474. /* e3 IS 9600/N/8/1   C3 IS 4800/N/8/1 */
  475. /* auxr->h.al = 0xC3 ;                    /* 4800,n,8,1 */
  476. /*   auxr->h.al = 0x33 ;                   /* 19.2,n,8,1 */
  477.    auxr->h.al = 0xe3 ;                   /* 9600,n,8,1 */
  478. /*   auxr->h.al = 0x9A ;                    /* 1200,e,7,1 */
  479. /*   auxr->h.al = 0xBA ;                     /* 2400 */
  480.  
  481.    int86( 0x14 , auxr , &retblk );                /* set up com port */
  482.  
  483.  printf("\nInitializing COM1 for %x (9a=1200,ba=2400e7,c3=4800n8,e3=9.6k,33=19.2)\n",
  484.      auxr->h.al);
  485.  
  486.    while (!AUXRDY())
  487.    {
  488.       printf("\n\n** TNC doesn't seem ready! (hardware flow control showing\
  489.  busy - check cable/TNC power etc.\n\007");
  490.       for ( x = 0 ; x < 30000 && !AUXRDY() && !kbhit() ; x++ ) ;
  491.    }
  492.  
  493.    signal( SIGINT, donada   );                /* kill ^C int */
  494.  
  495.    dwnfile = upfile = (FILE *) NULL ;               /* zap all file ptrs */
  496.  
  497.     aftr_init();
  498.         dlc_init();
  499.             dlc_start_cmd();    /** inits dlc**/
  500.         blp_init();
  501.  
  502.    PUTAUX( 'Q'-0x40 );            /* unassert possible STOP left by PTP */
  503.  
  504.  
  505.    /* do forever terminal program */
  506.    while ( 1 )
  507.    {
  508.     totnc_proc();
  509.     fmtnc_proc();
  510.     blp_tick_test();
  511.    }
  512.  
  513. }
  514.